/* 工具模块 */
// let env = 'dev';
const gulp = require('gulp'),
    // css2js = require('gulp-css2js'),
    // autoprefixer = require('gulp-autoprefixer'),
    cleanCSS = require('gulp-clean-css'), // css 压缩
    jshint = require('gulp-jshint'),
    uglify = require('gulp-uglify'), // js 压缩
    // htmlmin = require('gulp-htmlmin'),
    imagemin = require('gulp-imagemin'),
    pngquant = require('imagemin-pngquant'),
    rename = require('gulp-rename'),
    concat = require('gulp-concat'),
    // clean = require('gulp-clean'), // 清空文件夹
    // sass = require('gulp-sass'),
    cached = require('gulp-cached'), // 缓存文件到内存中，只传送修改的文件
    cache = require('gulp-cache'), // 缓存图片
    // remember = require('gulp-remember'), // 合并文件时，传送所有的文件
    // sourcemaps = require('gulp-sourcemaps'),
    rev = require('gulp-rev'), // - 对文件名加MD5后缀
    sequence = require('gulp-sequence'), // 设置任务是串行执行还是并行执行，中括号并行，圆括号串行
    revCollector = require('gulp-rev-collector'); // - 路径替换
    plumber = require('gulp-plumber'),//出现异常并不终止watch事件（gulp-plumber）
    notify = require('gulp-notify');//提示我们出现了错误（gulp-notify）
    // newer = require('gulp-newer'), // 只处理修改的文件
    // browserSync = require('browser-sync').create(),
    // reload = browserSync.reload,
    // filter = require('gulp-filter'); // 过滤文件

/**
 * 压缩图片
 */
gulp.task('img-unuse', function () {
    var imgSrc = './src/img/**/*',
        imgDst = './dist/img';
    return gulp.src(imgSrc)
        // .pipe(cached('img-min'))
        .pipe(imagemin({ optimizationLevel: 5, progressive: true, interlaced: true }))
        .pipe(gulp.dest(imgDst));
    // .pipe(livereload());
});
gulp.task('clear', function () {
    cached.caches = {};// 清除gulp-cached的缓存
    cache.clearAll(); // 清除gulp-cache的缓存
});
gulp.task('img', function() {
    var imgSrc = './src/img/**/*',
        imgDst = './dist/img';
    return gulp.src(imgSrc)
        .pipe(plumber({errorHandler:notify.onError('Error:<%= error.message %>')}))
        .pipe(cache(imagemin({
            optimizationLevel: 5, // 类型：Number  默认：3  取值范围：0-7（优化等级）
            progressive: true, // 类型：Boolean 默认：false 无损压缩jpg图片
            interlaced: true,
            svgoPlugins: [{removeViewBox: false}], // 不要移除svg的viewbox属性
            use: [pngquant()] // 使用pngquant深度压缩png图片的imagemin插件
        })))
        .pipe(gulp.dest(imgDst));
});
/**
 * 公用css样式压缩合并（不需要加MD5版本号，更新不需要改模板）
 */
gulp.task('css-common', function () {
    var cssSrc = [
            './src/css/base.css',
            './src/css/common.css'
        ],
        cssDst = './dist/css';
    return gulp.src(cssSrc)
        // .pipe(cached('css-common'))
        // .pipe(autoprefixer())
        // .pipe(remember('css-common'))
        .pipe(concat('common.css'))
        .pipe(rename({suffix: '.min'}))
        .pipe(cleanCSS())
        .pipe(gulp.dest(cssDst));
    // .pipe(reload({stream:true}));
});
gulp.task('css-6', function () {
    var cssSrc = [
            './src/css/reset.css',
            './src/css/redPocket.css'
        ],
        cssDst = './dist/css';
    return gulp.src(cssSrc)
        // .pipe(cached('css-common'))
        // .pipe(autoprefixer())
        // .pipe(remember('css-common'))
        .pipe(concat('page_6.css'))
        .pipe(rename({suffix: '.min'}))
        .pipe(cleanCSS())
        .pipe(gulp.dest(cssDst));
    // .pipe(reload({stream:true}));
});
gulp.task('css-page', function () {
    var cssSrc = [
            './src/css/page_redpackageh5.css',
            './src/css/page_redpackage.css'
        ],
        cssDst = './dist/css';
    return gulp.src(cssSrc)
        // .pipe(cached('css-common'))
        // .pipe(autoprefixer())
        // .pipe(remember('css-common'))
        .pipe(rename({suffix: '.min'}))
        .pipe(cleanCSS())
        .pipe(rev())
        .pipe(gulp.dest(cssDst))
        .pipe(rev.manifest())
        .pipe(gulp.dest('rev/css'));
    // .pipe(reload({stream:true}));
});
gulp.task('css-redpackageh5', function () {
    var cssSrc = [
            './src/css/page_redpackageh5.css'
        ],
        cssDst = './dist/css';
    return gulp.src(cssSrc)
        // .pipe(cached('css-common'))
        // .pipe(autoprefixer())
        // .pipe(remember('css-common'))
        .pipe(rename({suffix: '.min'}))
        .pipe(cleanCSS())
        .pipe(rev())
        .pipe(gulp.dest(cssDst))
        .pipe(rev.manifest())
        .pipe(gulp.dest('rev/css'));
    // .pipe(reload({stream:true}));
});
gulp.task('css-redpackage', function () {
    var cssSrc = [
            './src/css/page_redpackage.css'
        ],
        cssDst = './dist/css';
    return gulp.src(cssSrc)
        // .pipe(cached('css-common'))
        // .pipe(autoprefixer())
        // .pipe(remember('css-common'))
        .pipe(rename({suffix: '.min'}))
        .pipe(cleanCSS())
        .pipe(rev())
        .pipe(gulp.dest(cssDst))
        .pipe(rev.manifest())
        .pipe(gulp.dest('rev/css'));
    // .pipe(reload({stream:true}));
});
gulp.task('js-redpackage', ['js-copy'], function () {
    var jsSrc = [
            './src/js/entry.js',
            './src/js/totalmoney.js',
            './src/js/superAlert.js',
            './src/js/socket.io.js',
            './src/js/pomeloclient.js',
            './src/js/md5.min.js',
            './src/js/fastclick.min.js',
            './src/js/page_redpackage.js'
        ],
        jsDst = './dist/js/';

    gulp.src(jsSrc)
        .pipe(concat('redpackage.all.js'))
        .pipe(uglify())
        .pipe(jshint())
        .pipe(rev())
        .pipe(gulp.dest(jsDst))
        .pipe(rev.manifest())
        .pipe(gulp.dest('rev/js'));
    // .pipe(reload({stream:true}));
});
gulp.task('js-copy', function () {
    // 复制js到dist目录
    gulp.src('./src/js/jquery-3.2.1.min.js')
        .pipe(gulp.dest('./dist/js'));
});
gulp.task('js-redpackageh5', function () {
    var jsSrc = [
            './src/js/jquery-3.2.1.min.js',
            './src/js/fastclick.min.js',
            './src/js/page_redpackageh5.js'
        ],
        jsDst = './dist/js/';
    gulp.src(jsSrc)
        .pipe(concat('redpackageh5.all.js'))
        .pipe(uglify())
        .pipe(jshint())
        .pipe(rev())
        .pipe(gulp.dest(jsDst))
        .pipe(rev.manifest({
            merge: true
        }))
        .pipe(gulp.dest('rev/js'));
});
gulp.task('rev', function () {
    var revDst = './rev/**/*.json',
        htmlSrc = './dist/*.html',
        htmlDst = './dist/';
    gulp.src([revDst, htmlSrc])
        .pipe(revCollector({
            replaceReved: true // 设置replaceReved标识, 用来说明模板中已经被替换的文件是否还能再被替换,默认是false
        })) // - 执行文件内css名的替换
        .pipe(gulp.dest(htmlDst)); // - 替换后的文件输出的目录
    // .pipe(reload({stream:true}));
});
// 站外H5
gulp.task('page-test', sequence('css-page','rev'));
// 站外H5
gulp.task('page-redpackageh5', sequence(['js-redpackageh5', 'css-common', 'css-redpackageh5'],'rev'));
// 站内H5
gulp.task('page-redpackage', sequence(['js-redpackage', 'css-common', 'css-redpackage','css-6'],'rev'));
// 'img',
gulp.task('page-all', sequence(['js-redpackage', 'js-redpackageh5', 'css-common', 'css-page', 'css-6'],'rev'));